home *** CD-ROM | disk | FTP | other *** search
- Path: news.ahc.ameritech.com!datalytics!usenet
- From: Rob Stewart <stew@datalytics.com>
- Newsgroups: comp.lang.c++
- Subject: Re: (no subject)
- Date: Fri, 05 Apr 1996 18:44:07 -0500
- Organization: Datalytics, Inc
- Message-ID: <3165B047.12E6@datalytics.com>
- References: <4juvuk$4vn@newserv.ksu.ksu.edu>
- NNTP-Posting-Host: 204.62.224.71
- Mime-Version: 1.0
- Content-Type: text/plain; charset=us-ascii
- Content-Transfer-Encoding: 7bit
- X-Mailer: Mozilla 2.0 (WinNT; I)
-
- Iyer wrote:
- >
- > I have a problem in returning a two dimensional array.I have declared it
- > as a two dimensional pointer in my Matrix class.This is a private
- > variable.To access this i have two functions .One is
- > void SetMatrixElements(double **mat)
- > double **GetMatElements();
- > When i receive this GetMatElements(),i am declaring a double **temp,
- > then I am receiving in this temp pointer.But i am not allocating any
- > memory for this temp pointer.When i delete the the Matrix object, it's
- > mat elements get deleted,but to which this temp is pointing.But i cannot
- > delete the temp before i quit the function,this will delete the mat
- > elements.So, what shall i do.
- >
- > class Matrix
- > {
- > private:
- > int nc;int nr; double **mat;
- > public:
- > double **GetMatElemnts();
- > void SetMatElemnts(double **m);
- >
- > }
- >
- > Matrix *A;
- > void Function(){
- > double **temp;
- > temp = A->GetMatElements();
- >
- > }
-
- Returning a non-const pointer to a private dm is poor practice.
- That aside, the problem is that GetMatElements is returning a
- pointer to the same heap allocation as Matrix::mat points to.
- If you choose to retain the current design, you will need
- GetMatElements to allocate and populate a second array as a copy
- of mat. It can then return the pointer to this copy. As
- parallel behavior, SetMatElements must first "delete [] mat"
- before assigning m to mat.
-
- This implementation causes an ownership problem. If you call
- GetMatElements to get a pointer to an array, modify one or more
- elements, then call SetMatElements with that pointer, who owns
- the memory? The caller has a pointer from GetMatElements which
- is known to be a copy of Matrix's mat data member. Therefore,
- the caller should "delete [] temp." However, the caller used
- that pointer (temp) in a call to SetMatElements. SetMatElements
- then deleted its previous matrix and set mat to point to the
- matrix given to it. Therefore, SetMatElements took ownership of
- the memory.
-
- The only safe implementation for this is to use a vector class
- to manage the data. vector is part of STL, so it's reasonable
- to expect that you don't have it available. However, you can
- write your own one dimensional array class overloading operator
- [] to access the elements. The result would be the following
- declaration:
-
- vector<vector<double>> matrix;
-
- To access a specific element, you simply do this:
-
- matrix[n][m]
-
- Now, you can read and write individual elements with clear
- syntax just as you would write them on paper.
-
- --
- Robert Stewart | My opinions are usually my own.
- Datalytics, Inc. | stew@datalytics.com
-